package javaStudy.反射;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
/*
通过反射在ArrayList<Integer>集合中添加字符串
 */
public class Demo3 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ArrayList<Integer> array=new ArrayList<>();
        array.add(10);
        array.add(20);
        array.add(30);
        Class<? extends ArrayList> aClass = array.getClass();
        Method add = aClass.getMethod("add", Object.class);
        add.invoke(array,"yunhi");
        add.invoke(array,"hailin");
        System.out.println(array);
    }
}
